Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

class-validator

Package Overview
Dependencies
Maintainers
5
Versions
58
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

class-validator

Decorator-based property validation for classes.

  • 0.13.2
  • Source
  • npm
  • Socket score

Version published
Maintainers
5
Created

What is class-validator?

The class-validator package is a TypeScript and JavaScript library that allows for validation of classes and objects using decorators and non-decorator based validation. It is commonly used to ensure data integrity before persistence or processing and is particularly useful when working with class-based models.

What are class-validator's main functionalities?

Declarative Validation Decorators

Allows the use of decorators to declare validation rules directly in the class model. For example, ensuring a property is not empty, is an integer, or is a valid email address.

{"@IsNotEmpty()": "public name: string;", "@IsInt()": "public age: number;", "@IsEmail()": "public email: string;"}

Custom Validation Classes

Enables the creation of custom validation classes that implement the ValidatorConstraintInterface. This allows for complex and custom validation logic that can be reused across different class properties.

{"@ValidatorConstraint({ async: false })": "class IsLongerThanConstraint implements ValidatorConstraintInterface { validate(text: string, args: ValidationArguments) { return text.length > args.constraints[0]; } defaultMessage(args: ValidationArguments) { return 'Text is too short. Should be longer than ' + args.constraints[0] + ' characters'; } }", "@ValidatorConstraint()": "@IsLongerThan(10, { message: 'Text must be longer than 10 characters' }) public text: string;"}

Validation of Nested Objects

Supports validation of nested objects using the ValidateNested decorator, ensuring that the properties of the nested objects also adhere to their respective validation rules.

{"@ValidateNested()": "@Type(() => SubClass) public nested: SubClass;"}

Asynchronous Validation

Provides asynchronous validation functions like validateOrReject, which can handle async validators and return a promise that either resolves if the object is valid or rejects with the validation errors.

{"@IsEmail({}, { message: 'Invalid email' })": "public email: string;", "validateOrReject(someObject)": ".catch(errors => { console.log('Promise rejected (validation failed). Errors: ', errors); });"}

Manual Validation

Allows for manual validation of objects without using decorators by calling the validate function, which returns a promise containing any validation errors.

{"validate(someObject)": ".then(errors => { if (errors.length > 0) { console.log('Validation failed: ', errors); } else { console.log('Validation succeed'); } });"}

Other packages similar to class-validator

FAQs

Package last updated on 20 Nov 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc